﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBQLTV;

namespace DAO
{
    public class DauSachDAO
    {
        public List<string> GetListDauSach()
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                List<string> result = new List<string>();
                List<DAUSACH> lDauSach = entity.DAUSACHes.ToList();
                foreach (DAUSACH dausach in lDauSach)
                {
                    result.Add(dausach.TenSach);
                }
                return result;
            }
        }
        public bool InsertDauSach(string TenSach, THELOAISACH TheLoai, List<string> TenTacGia)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DAUSACH dausach_temp = entity.DAUSACHes.FirstOrDefault(o => o.TenSach == TenSach && o.MaTheLoai == TheLoai.MaTheLoaiSach);
                if (dausach_temp != null)
                    return false;

                DAUSACH dausach = new DAUSACH()
                {
                    TenSach = TenSach,
                    MaTheLoai = TheLoai.MaTheLoaiSach,
                };

                foreach (string string_temp in TenTacGia)
                {
                    TACGIA tacgia = TacGiaDAO.GetTacGiaByName(string_temp);
                    if (tacgia != null && !dausach.TACGIAs.Contains(tacgia))
                    {
                        entity.TACGIAs.Attach(tacgia);
                        dausach.TACGIAs.Add(tacgia);
                    }
                }
                entity.DAUSACHes.AddObject(dausach);
                entity.SaveChanges();
                return true;
            }
        }
        public static DAUSACH GetDauSachByName(string TenSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DAUSACH dausach = entity.DAUSACHes.FirstOrDefault(o => o.TenSach == TenSach);
                return dausach;
            }
        }
        public string GetTenSachByKey(int MaDauSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DAUSACH dausach = entity.DAUSACHes.FirstOrDefault(o => o.MaDauSach == MaDauSach);
                if (dausach != null)
                    return dausach.TenSach;
                else
                    return null;
            }

        }
        public THELOAISACH GetTheLoaiSach(int MaDauSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DAUSACH dausach = entity.DAUSACHes.FirstOrDefault(o => o.MaDauSach == MaDauSach);
                if (dausach != null)
                    return dausach.THELOAISACH;
                else
                    return null;
            }
        }
        public DAUSACH GetDauSach(int MaSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.MaSach == MaSach);
                List<DAUSACH> result = new List<DAUSACH>();
                return sach.DAUSACH;
            }
        }
    }
}
